<?php
/**
 * Created by PhpStorm.
 * User: adminyj
 * Date: 2019/2/16
 * Time: 20:51
 */

namespace app\admin\controller;


use app\api\controller\Jpushmember;
use app\common\controller\Adminbase;
use think\Db;

class Volunteer extends Adminbase
{


    /**
     * 志愿者申请列表
     * */
    public function index()
    {

        if ($this->request->isAjax()) {


            $limit = $this->request->param('limit/d', 10);
            $page = $this->request->param('page/d', 10);
            $where = [];
            $order = "id DESC";

            $mobile = input('mobile');
            if($mobile){
                $where[] = ['mobile','like','%'.$mobile.'%'];
            }
            $time = input('time');
            if($time){
                $start_time = substr($time, 0, 20);//订单那创建开始时间
                $end_time = substr($time, 21, 20);//订单那创建开始时间
                $time = strtotime($start_time);
                $times = strtotime($end_time);
                $where[] = ['create_time','between',$time.','.$times];
            }
            $status = input('status');
            if($status){
                if($status == -1){
                    $where[] = ['status','=',0];
                }else{
                    $where[] = ['status','=',$status];
                }
            }else{
                $where[] = ['status','>=',0];
            }

            $list = Db::view('volunteer','*')
                ->view('political','value','volunteer.political = political.id','left')
                ->view('political as a','value as aValue','volunteer.political_info = political.id','left')
                ->view('zhibu','value as bValue','volunteer.zhibu = zhibu.id','left')
                ->where($where)
                ->page($page,$limit)
                ->order($order)
                ->withAttr('create_time', function ($value, $data) {
                    if($value){
                        return date('Y-m-d H:i:s', $value);
                    }else{
                        return "--";
                    }
                })
                ->withAttr('aValue', function ($value, $data) {
                    if(!$value){
                        return "无";
                    }
                })
                ->select();
            foreach ($list as $k => $v){
                if($v['status'] == 0){
                    $list[$k]['statusName'] = "未审核";
                }else if($v['status'] == 1){
                    $list[$k]['statusName'] = "审核通过";
                }else if($v['status'] == -1){
                    $list[$k]['statusName'] = "审核未通过";
                }
            }
            $total = Db::name('volunteer')
                ->where($where)
                ->order($order)
                ->count();
            $result = array("code" => 0, "count" => $total, "data" => $list);
            return json($result);
        }
        return $this->fetch();

    }


    /**
     *
     * 志愿者审核
     *
     * */
    public function examine()
    {

        $id = input('id');
        if(!$id){
            $this->error('获取数据失败');
        }
        $uid = Db::name('volunteer')->where(['id' => $id])->value('user_id');
        $data = array(
            'status' => 1
        );
        $res = Db::name('volunteer')->where(['id' => $id])->update($data);
        if($res){
            $up = [
                'role_id' => 3
            ];
            Db::name('member')->where(['id' => $uid])->update($up);

            $jPush = new Jpushmember();

            $user = [];

            $info = Db::name('volunteer')->where(['id' => $id])->field('user_id')->find();
            array_push($user,$info['user_id']);
            $content = "您的志愿者申请已通过";
            $result = $jPush->pushAlias($user,$content);

            if($result['http_code'] == 200){
                $maps[] = ['apply_id','=',$id];
                $maps[] = ['type','=',3];
                $helpCount = Db::name('send_msg')->where($maps)->find();
                if(count($helpCount)){
                    $upData['is_check'] = 0;
                    $reson = Db::name('send_msg')->where(['id' => $helpCount['id']])->update($upData);
                }else{

                    $inData = [
                        'user_id' => $info['user_id'],
                        'apply_id' => $id,
                        'title' => '您的志愿者申请有了新进度',
                        'content' => "志愿者申请已通过",
                        'create_time' => time(),
                        'is_check' => 0,
                        'type' => 3
                    ];
                    $reson = Db::name('send_msg')->where(['id' => $helpCount['id']])->insert($inData);
                }
            }

            $this->error('审核成功');
        }else{
            $this->error('审核失败');
        }

    }


    /**
     * 拒绝操作
     * */
    public function refuse()
    {

        $id = input('id');
        if(!$id){
            $this->error('获取数据失败');
        }

        $data = array(
            'status' => -1
        );
        $res = Db::name('volunteer')->where(['id' => $id])->update($data);
        if($res){

            $jPush = new Jpushmember();

            $user = [];

            $info = Db::name('volunteer')->where(['id' => $id])->field('user_id')->find();
            array_push($user,$info['user_id']);
            $content = "您的志愿者申请已被拒绝";
            $result = $jPush->pushAlias($user,$content);

            if($result['http_code'] == 200){
                $maps[] = ['apply_id','=',$id];
                $maps[] = ['type','=',3];
                $helpCount = Db::name('send_msg')->where($maps)->find();
                if(count($helpCount)){
                    $upData['is_check'] = 0;
                    $reson = Db::name('send_msg')->where(['id' => $helpCount['id']])->update($upData);
                }else{

                    $inData = [
                        'user_id' => $info['user_id'],
                        'apply_id' => $id,
                        'title' => '您的志愿者申请有了新进度',
                        'content' => "志愿者申请已被拒绝",
                        'create_time' => time(),
                        'is_check' => 0,
                        'type' => 3
                    ];
                    $reson = Db::name('send_msg')->where(['id' => $helpCount['id']])->insert($inData);
                }
            }

            $this->success('拒绝成功');
        }else{
            $this->error('拒绝失败');
        }

    }


    /**
     * 拒絕申請
     * */
    public function fail()
    {

        $id = input('id');
        if(!$id){
            $this->error('获取数据有误');
        }
        if($this->request->isPost()){

            $id = input('id');
            $beizhu = input('beizhu','');
            if(!$beizhu){
                $this->error('备注不能为空');
            }
            $data['beizhu'] = $beizhu;
            $data['status'] = -1;
            $res = Db::name('volunteer')->where(['id' => $id])->update($data);

            if($res){

                $user = [];

                $info = Db::name('volunteer')->where(['id' => $id])->field('user_id,pass_msg')->find();
                array_push($user,$info['user_id']);
                $jPush = new Jpushmember();

                $content = "您的志愿者申请被拒绝：".$beizhu;
                $result = $jPush->pushAlias($user,$content);

                if($result['http_code'] == 200){
                    $maps[] = ['apply_id','=',$id];
                    $maps[] = ['type','=',3];
                    $helpCount = Db::name('send_msg')->where($maps)->find();
                    if(count($helpCount)){
                        $upData['is_check'] = 0;
                        $reson = Db::name('send_msg')->where(['id' => $helpCount['id']])->update($upData);
                    }else{

                        $inData = [
                            'user_id' => $info['user_id'],
                            'apply_id' => $id,
                            'title' => '您的志愿者申请有了新进度',
                            'content' => $beizhu,
                            'create_time' => time(),
                            'is_check' => 0,
                            'type' => 3
                        ];
                        $reson = Db::name('send_msg')->where(['id' => $helpCount['id']])->insert($inData);
                    }
                }

                $this->success('操作成功');
                //$result = array("code" => 1, "msg" => "操作成功", "data" => [],"url" => "/admin/applyhelp/index");
            }else{
                $this->error('操作失败');
                //$result = array("code" => -1, "msg" => "操作失败", "data" => []);
            }
        }else{
            $this->assign('id',$id);
            return $this->fetch();
        }

    }

}